﻿namespace imichaelmiers.wp7.security.cryptography.PasswordProtectedStorage
{
    using System.Runtime.Serialization;
    using System.Security.Cryptography;

    /// <summary>
    /// Metadata used for encrypting and decrypting the object. 
    /// This is public data. 
    /// Public to allow for serialization. 
    /// However, the constructor is internal to prevent 
    /// use outside of this code.
    /// </summary>
    [DataContract]
    public class PublicCryptographicMetaData
    {
        /// <summary>
        ///  Public for serialization purposes only. 
        /// </summary>
        public const int KeySizeInBytes = 256 / 8;

        /// <summary>
        ///  Public for serialization purposes only. 
        /// </summary>
        public const int BlockSizeInBytes = 128 / 8;

        /// <summary>
        ///  Public for serialization purposes only. 
        /// </summary>
        [DataMember]
        public readonly byte[] IV;

        /// <summary>
        ///  Public for serialization purposes only. 
        /// </summary>
        [DataMember]
        public readonly byte[] SaltForMacKey;

        /// <summary>
        ///  Public for serialization purposes only. 
        /// </summary>
        [DataMember]
        public readonly byte[] SaltForEncryptionKey;

        internal PublicCryptographicMetaData()
        {
            IV = new byte[BlockSizeInBytes];
            SaltForMacKey = new byte[KeySizeInBytes];
            SaltForEncryptionKey = new byte[KeySizeInBytes];
            RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
            rngCsp.GetBytes(IV);
            rngCsp.GetBytes(SaltForEncryptionKey);
            rngCsp.GetBytes(SaltForMacKey);
        }
    }
}
